/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BASE_ALGO_MANAGER_H
#define BASE_ALGO_MANAGER_H

#include "utils_noncopyable.h"
#include "nlohmann/json.hpp"
#include <memory>
#include <string>
#include <functional>
#include <vector>
#include <cstdint>
#include "hal_algo.h"

namespace El {
namespace Algo {

/**
 * @brief 算法类型枚举
 */
using AlgoType = HAL_ALGO_TYPE_E;
constexpr AlgoType ALGO_TYPE_INVALID = ALGO_TYPE_NUM_MAX;

/**
 * @brief 算法状态枚举
 */
enum class AlgoStatus : int32_t {
    IDLE = 0,     // 空闲状态
    INITIALIZING, // 初始化中
    RUNNING,      // 运行中
    STOPPING,     // 停止中
    ERROR         // 错误状态
};

/**
 * @brief 算法管理器接口类
 */
class IAlgoManager {
public:
    /**
     * @brief 算法结果回调函数类型
     * @param result JSON格式的算法识别结果
     */
    using ResultCallback = std::function<void(const nlohmann::json &result)>;

    /**
     * @brief 获取算法管理器单例实例
     * @return 算法管理器实例引用
     */
    static IAlgoManager &GetInstance();

    virtual ~IAlgoManager() = default;

    /**
     * @brief 初始化算法管理器，根据配置文件自动启动算法
     * @return 成功返回true，失败返回false
     */
    virtual bool Start() = 0;

    /**
     * @brief 反初始化算法管理器
     */
    virtual void Stop() = 0;

    /**
     * @brief 启动指定算法
     * @param algoType 算法类型
     * @return 是否启动成功
     */
    virtual bool StartAlgo(AlgoType algoType) = 0;

    /**
     * @brief 停止指定算法
     * @param algoType 算法类型
     * @return 成功返回true，失败返回false
     */
    virtual bool StopAlgo(AlgoType algoType) = 0;

    /**
     * @brief 获取算法状态
     * @param algoType 算法类型
     * @return 算法状态
     */
    virtual AlgoStatus GetAlgoStatus(AlgoType algoType) const = 0;

    /**
     * @brief 设置算法结果回调函数
     * @param callback 回调函数，传nullptr清除回调
     *
     * 回调函数示例:
     * @code
     * auto callback = [](const nlohmann::json &result) {
     *     std::cout << "Algorithm result: " << result.dump(2) << std::endl;
     *
     *     // 访问JSON字段
     *     if (result.contains("algorithm_type")) {
     *         int algoType = result["algorithm_type"];
     *         std::cout << "Algorithm type: " << algoType << std::endl;
     *     }
     *
     *     if (result.contains("object_count")) {
     *         int count = result["object_count"];
     *         std::cout << "Detected objects: " << count << std::endl;
     *     }
     * };
     * manager.SetResultCallback(callback);
     * @endcode
     */
    virtual void SetResultCallback(ResultCallback callback) = 0;

    /**
     * @brief 设置算法置信度阈值（便捷方法）
     * @param algoType 算法类型
     * @param threshold 置信度阈值
     * @return 是否成功
     */
    virtual bool SetConfidenceThreshold(AlgoType algoType, float threshold) = 0;
};

/**
 * @brief 算法类型转换为字符串
 * @param algoType 算法类型
 * @return 算法类型字符串
 */
std::string AlgoTypeToString(AlgoType algoType);

/**
 * @brief 字符串转换为算法类型
 * @param str 算法类型字符串
 * @return 算法类型
 */
AlgoType StringToAlgoType(const std::string &str);

/**
 * @brief 算法状态转换为字符串
 * @param status 算法状态
 * @return 算法状态字符串
 */
std::string AlgoStatusToString(AlgoStatus status);

} // namespace Algo
} // namespace El

#endif // BASE_ALGO_MANAGER_H